/**
 * Author: w景洋.
 * Time: 2016/4/27 0027
 */
angular.module('usercenter.module.user', [])
    .config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $stateProvider
                .state('ucmodel.user', {
                    url: '/user',
                    templateUrl: usercenter_templateprefix + 'user/user.html',
                    controller: 'UserController'
                })
                .state('grzx', {
                    url: '/grzx',
                    templateUrl: usercenter_templateprefix + 'user/grzx.html',
                    controller: 'grzxController'
                })
                .state('zlxg', {
                    url: '/zlxg',
                    templateUrl: usercenter_templateprefix + 'user/zlxg.html',
                    controller: 'zlxgController'
                })
                .state('about', {
                    url: '/about',
                    templateUrl: usercenter_templateprefix + 'user/about.html',
                    controller: 'AboutController'
                })

        }])
    //用户信息
    .controller('UserController', ['$scope', '$state', 'usercenter_AuthService', function ($scope, $state, usercenter_AuthService) {
        lazy();
        UserChange(3);
        $scope.uc_mallurl = projectBaseUrl;
        var id = usercenter_AuthService.getUserid();
        if (id == 0 || id == undefined) {
            $state.go('login');
        }

        $scope.leave = function () {
            usercenter_AuthService.logout();
            $state.go('login');
        }
        $scope.toRecgoods = function (active) {
            usercenter_AuthService.getUserid();
            $state.go('grzx', {active: active});
        }

    }])
    .controller('grzxController', ['$scope', '$state', function ($scope, $state) {
        $scope.ent_mallurl = projectBaseUrl;
    }])
    .controller('zlxgController', ['$scope', '$state', '$rootScope', '$localStorage', 'usercenter_AuthService', 'fileReader', function ($scope, $state, $rootScope, $localStorage, usercenter_AuthService, fileReader) {
        var $storage = $localStorage.$default({
            cmtname: '',
        });
        $scope.username = usercenter_AuthService.getUsername();
        usercenter_AuthService.getMessg().$promise.then(function (data) {
            $scope.user = data;
        });
        //上传照片
        $scope.getFile = function () {
            fileReader.readAsDataUrl($scope.file, $scope)
                .then(function (result) {
                    $scope.imageSrc = result;
                    $scope.user.logo = $scope.imageSrc
                });
        };
        $scope.savehead = function () {
            var promise = usercenter_AuthService.uploadFile($scope.imageSrc, $scope.myFile);
            promise.then(function (data) {
                $scope.user.logo = data;
                usercenter_AuthService.setUserhead($scope.user)
            })
        }
        $scope.disupload = function () {
            usercenter_AuthService.getMessg().$promise.then(function (data) {
                $scope.user = data;
            })
        }
        $scope.sure = function () {
            var nam = $(".newname").val();
            var fist = nam.substring(1, 0);
            if (nam.length == 0) {
                input_alert("用户名不能为空！")
            }
            else if (nam.length < 2 || nam.length > 16) {
                input_alert("用户名限2-16字符之间！")
            }
            else if (!fist.match(/^[\u4E00-\u9FA5a-zA-Z]/)) {
                input_alert("用户名只能以英文字母或汉字开头！")
            }
            else if (!nam.match(/^[\u4E00-\u9FA5a-zA-Z0-9]+$/)) {
                input_alert("用户名不能为特殊符号！")
            } else {
                $scope.user.name = $scope.username;
                usercenter_AuthService.resetName($scope.user, function () {
                    $storage.cmtname = $scope.user.name;
                    $scope.username = $scope.user.name;
                    input_alert('修改成功', 1000);
                }, function () {
                })
            }
        }
    }])
    //关于我们
    .controller('AboutController', ['$scope', function ($scope) {

    }])